package com.hudilao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hudilao.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 用户付款后更新订单相关信息
     * @param id  订单id
     * @param status  订单状态
     * @param paymentMethod  订单支付方式
     * @param completeTime  订单完成时间
     */
    @Update("update orders set status=#{status}, payment_method=#{paymentMethod}, complete_time=#{completeTime} where id=#{id}")
    void updateByIdAfterPay(long id, Integer status, Integer paymentMethod, LocalDateTime completeTime);

    /**
     * 根据用户id获取其历史订单
     * @param customerId  顾客id
     * @return  根据顾客id查找到的其历史订单
     */
//    @Select("select * from orders where customer_id=#{customerId}")
    @Select("SELECT o.* "+
            "FROM orders o "+
            "JOIN customer c ON o.customer_id = c.id "+
            "WHERE o.customer_id = #{customerId} "+
            "ORDER BY create_time DESC")
    List<Order> getOrdersById(long customerId);

    /**
     * 用户评价订单后更新其状态
     * @param id  订单id
     * @param status  订单状态
     */
    @Update("update orders set status=#{status} where id=#{id}")
    void updateByIdAfterReview(long id,Integer status);

    /**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param createTime
     * @return
     */
    @Select("select * from orders where status = #{status} and create_time < #{createTime}")
    List<Order> getByStatusAndCreateTimeLessThan(Integer status, LocalDateTime createTime);


    //管理端：订单数目统计
    /**
     * 根据订单状态统计订单数量
     * @param status 订单状态
     * @return int 该状态下的订单数量
     */
    @Select("SELECT COUNT(*) FROM orders WHERE status = #{status}")
    int countByStatus(int status);

    /**
     * 统计所有订单的数量
     * @return int 总订单数量
     */
    @Select("SELECT COUNT(*) FROM orders")
    int countTotalOrders();
}
